/**
 * 
 */
package com.sci.web.action.login;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.sci.web.action.util.BaseAction;
import com.sci.web.bean.IndustryStaticsEnterprise;
import com.sci.web.bean.Options;
import com.sci.web.bean.Permission;
import com.sci.web.bean.StaticsEnterpriseByCityId;
import com.sci.web.bean.User;
import com.sci.web.bean.UserLoginTime;
import com.sci.web.dao.UserLoginDao;

/**
 * @author BaiFuyou
 * @date: Sep 22, 2011 3:56:25 PM
 */
public class UserLoginAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6129599451464557888L;
	private String name;
	private String pwd;
	
	/**
	 * 向页面传值
	 */
	private boolean success = false;
	/**
	 * 向页面传值属性
	 */
	private String message = "";
	@Resource
	private UserLoginDao userlogindao;

	/**
	 * @return the name
	 */
	@JSON(serialize = false)
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name.trim();
	}

	/**
	 * @return the pwd
	 */
	@JSON(serialize = false)
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd.trim();
	}
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		User u = new User();
		u.setName(this.getName());
		u.setPwd(this.getPwd());
		System.out.println(this.getName()+"  ============================================================== "+this.getPwd());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
		String nowDate = sdf.format(new Date()).toString();		 
		User loginUser = userlogindao.isUserExist(u);
		Options op = userlogindao.getOneOptions().get(0);
		List<IndustryStaticsEnterprise> listinstry = userlogindao.getAllIndustryStaticsEnterpriseForStatics();
		Permission p = null;
		String name = "";
		if(loginUser != null){
			/**
			 * 将用户放进全局变量，该用户用具有权限变量为树的形成做准备
			 */
			if(loginUser.getUser_is_delete() == 1){
				/**
				 * 表示该用户被删除了
				 */
				this.success = true;
				this.message = "delete";
				return "json";
			}
//			System.out.println(loginUser.getName()+"=====================");
			if(loginUser.getUser_is_enterprise() == 1){
				name = userlogindao.getEnterpriseName(loginUser.getUser_id());
			}else{
				name = loginUser.getName();
			}
			
			ActionContext.getContext().getSession().put("user", loginUser);	
			ActionContext.getContext().getSession().put("name", name);	
			p = userlogindao.getPermission(loginUser.getUser_role());
			ActionContext.getContext().getSession().put("serverdate",nowDate);
			/*
			 * 将权限放进变量
			 */
			ActionContext.getContext().getSession().put("permission",p);
			ActionContext.getContext().getSession().put("uc",loginUser.getUser_city());
			ActionContext.getContext().getSession().put("ur",loginUser.getUser_role());
			ActionContext.getContext().getSession().put("options",op);
			
			UserLoginTime ult = new UserLoginTime();
			ult.setLogintime(new Date());
			ult.setUsename(name+",IP:"+this.getIpAddr(this.getRequest()));
			/**
			 * 记录用户的登录信息
			 */
			userlogindao.userLoginTime(ult);
			List<StaticsEnterpriseByCityId> list = userlogindao.getAllEnterpriseStatics();
			ActionContext.getContext().getSession().put("xmldata",getEnterpriseJson(list) );
			ActionContext.getContext().getSession().put("xmldatainstry",getlistinstryJson(listinstry) );
			this.success = true;
//			this.message = ;
			this.message = "ok";
		}else{
			this.success = false;
			this.message = "fail";
		}
		
		return "json";
	}

	/**
	 * @return the success
	 */
	public boolean isSuccess() {
		return success;
	}

	/**
	 * @param success the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * 获取登录机器的ip地址
	 * 
	 * @param request
	 * @return
	 */
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	public String getEnterpriseJson(List<StaticsEnterpriseByCityId> list){
		String json = "<chart caption=\"\"   xAxisName=\"\" yAxisName=\"\" rotateYAxisName=\" 1\"   showValues=\" 1\"  decimalPrecision=\"0\" showNames=\"1\"  baseFontSize=\"12\" outCnvBaseFontSiz=\"20\" numberSuffix=\"个\" pieSliceDepth=\"30\" formatNumberScale=\"0\">";
		StringBuffer sb = new StringBuffer("");
		for(StaticsEnterpriseByCityId s:list){
			sb.append("<set name=\"");
			sb.append(s.getCname() + "\" ");
			sb.append("value=\"" + s.getValue() + "\"/>");
		}
		String end = "</chart>";
		String result = json + sb.toString() + end;
		System.out.println(result);
		return result;
	}
	public String getlistinstryJson(List<IndustryStaticsEnterprise> list){
		String json = "<chart caption=\"\"   xAxisName=\"\" yAxisName=\"\" rotateYAxisName=\" 1\"   showValues=\" 1\"  decimalPrecision=\"0\" showNames=\"1\"  baseFontSize=\"12\" outCnvBaseFontSiz=\"20\" numberSuffix=\"个\" pieSliceDepth=\"30\" formatNumberScale=\"0\">";
		StringBuffer sb = new StringBuffer("");
		for(IndustryStaticsEnterprise s:list){
			sb.append("<set name=\"");
			sb.append(s.getName() + "\" ");
			sb.append("value=\"" + s.getValue() + "\"/>");
		}
		String end = "</chart>";
		String result = json + sb.toString() + end;
		System.out.println(result);
		return result;
	}
}
